import RescacheMgr from "../../framework/inc/rescachemgr";

const SNOW_CNT = 1000;

const genSnowPos = function (sizew, sizeh) {
    return [
        Math.random() * sizew,
        Math.random() * sizeh,
    ]
}

export default class Weather extends THREE.Object3D {
    constructor(skyheight, sizerad) {
        super();
        let inc = RescacheMgr.getInstance();
        let vertex = inc.getGLSL("weathersnow_vertex");
        let fragment = inc.getGLSL("weathersnow_fragment");
        let material_shader = new THREE.ShaderMaterial(
            {
                uniforms: this.init_uniform(skyheight),
                vertexShader: vertex,
                fragmentShader: fragment,
                vertexColors: true
            }
        )
        let geometry_buffer = new THREE.BufferGeometry(); //pos 
        let ranpos = [];
        let sizes = [];
        let colors = [];
        let times = [];
        let color = new THREE.Color();
        let ranp;
        for (let i = 0; i < SNOW_CNT; i++) {
            ranp = genSnowPos(sizerad, sizerad);
            ranpos.push(ranp[0]);
            ranpos.push(ranp[1]);
            ranpos.push(0);
            sizes.push(10.0);
            times.push(0);
            
            color.setHSL(Math.random(0, 1), 1.0, 0.5);
            colors.push(color.r, color.g, color.b);
        }   
        geometry_buffer.setAttribute("position", new THREE.Float32BufferAttribute(ranpos, 3));
        geometry_buffer.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
        geometry_buffer.setAttribute("size", new THREE.Float32BufferAttribute(sizes, 1));
        geometry_buffer.setAttribute("time", new THREE.Float32BufferAttribute(times, 1));

        let particle = new THREE.Points(geometry_buffer, material_shader);
        this.add(particle);
        this._material = material_shader;
        this._gemometry = geometry_buffer;
    }

    update() {
        if (this._material) {
            let time = Date.now();
            // let pos = this._gemometry.attributes.position.array;
            // console.log()
        }
    }

    init_uniform(skyh) {
        let tp_snow = RescacheMgr.getInstance().getTexture("tp_snow");
        let uniforms = {
            texturesnow: {
                type: "t",
                value: tp_snow
            },
            time: {
                type: "f",
                value: 0.0
            },
            skyheight: {
                type: "f",
                value: skyh
            }
        };
        return uniforms;
    }
}